<?php
namespace app\index\controller;
use think\Db;
use think\Session;
use think\Controller;
use think\Request;
class User extends Wechat
{
	public function index(Request $request){
		return $this->view->fetch();
	}
	public function login(){

	}
	public function loginout()
	{
		Session::delete('wechat_user');
		$this->success('退出成功', 'index/index');
	}
	public function chit()
	{
		$openid=Session::get('wechat_user');
		$user=get_user_openid($openid);
		$class=input('id');
		if($class!=1 && $class!=2 && $class!=3){$this->error('无效链接',url('index/index'));}
		$where['type']=$class;
		$where['userid']=$user['id'];
		$order=Db::name('order')->where($where)->order('id desc')->select();

		$pay_type=array('1' =>'代金卷','2' =>'礼品卷','3' =>'积分商品');
		foreach ($order as $k => $v) {
			$order[$k]['pay_type']=$pay_type[$v['type']];
			if($v['type']==1 || $v['type'] ==2){
				$int='元';
			}else{
				$int='积分';
			}
			$order[$k]['pay_money']=$v['pay'].$int;

			$order[$k]['qr']=qrcode($v['key']);
			$order[$k]['bar']=barcode($v['key']);


			if($v['state']== '0'){
				$order[$k]['state_type']='已使用';
				$tab_order_0[]=$order[$k];
			}else if($v['state']== '1'){
				$order[$k]['state_type']='未使用';
				$tab_order_1[]=$order[$k];
			}else if($v['state']== 'p0'){
				$order[$k]['state_type']='未付款';
				$order[$k]['key']='';
				$tab_order_p0[]=$order[$k];
			}

		}

		$this->view->assign('tab_order_0', $tab_order_0);
		$this->view->assign('tab_order_1', $tab_order_1);
		$this->view->assign('tab_order_p0', $tab_order_p0);
		$this->view->assign('order', $order);
		return $this->view->fetch();
	}
	public function orderpay()
	{
		$id=input('id');
		$order=Db::name('order')->find($id);
		if($order['state'] != 'p0'){
			return 101;
		}

		$shop['money']=$order['pay'];
		$shop['id']=$id;
		
		SESSION::delete('cart');
		$cart=SESSION::get('cart');
		$cart[]=$shop;
		SESSION::set('cart',$cart);
		return 1;
	}
	public function success_pay(){
		$id=input('id');
		$order=Db::name('order');
		$where['id']=$id;
		$save['state']=1;
		$order->where($where)->update($save);
	}
	public function delorder()
	{
		$id=input('id');
		$order=Db::name('order')->find($id);
		
		if(!$order){
			$this->error('订单不存在',url('user/index'));
		}else if($order['state'] != 'p0'){
			$this->error('删除异常',url('user/index'));
		}
		$info=Db::name('order')->delete($id);
		if($info){
			$this->success('删除成功',url('user/index'));
		}else{
			$this->error('删除异常',url('user/index'));
		}
	}
	public function show()
	{
		$id=input('id');
		$openid=Session::get('wechat_user');
		$user=get_user_openid($openid);//用户信息

		$orderDb=Db::name('order');
		$order=$orderDb->find($id);//订单信息
		$order['qr']=qrcode($order['key']);
		$order['bar']=barcode($order['key']);

		if($order['type']==1 || $order['type'] ==2){
				$int='元';
		}else{
				$int='积分';
		}

		if($order['state']== '0'){
				$order['state_type']='已使用';
			}else if($order['state']== '1'){
				$order['state_type']='未使用';
			}else if($order['state']== 'p0'){
				$order['state_type']='未付款';
				$order['key']='';
			}

		$order['pay_money']=$order['pay'].$int;

		//$this->view->assign('user', $user);
		$this->view->assign('order', $order);
		return $this->view->fetch();
	}
	public function pay()
	{
		$openid=Session::get('wechat_user');
		$user=get_user_openid($openid);


		$where['userid']=$user['id'];
		
		$order=Db::name('order')->where($where)->order('id desc')->select();
		
		$pay_type=array('1' =>'代金卷','2' =>'礼品卷','3' =>'积分商品');
		foreach ($order as $k => $v) {
			$order[$k]['pay_type']=$pay_type[$v['type']];
			if($v['type']==1 || $v['type'] ==2){
				$int='元';
			}else{
				$int='积分';
			}
			$order[$k]['pay_money']=$v['pay'].$int;
			$order[$k]['qr']=qrcode($v['key']);

			if($v['state']== '0'){
				$order[$k]['state_type']='已使用';
				$tab_order_0[]=$order[$k];
			}else if($v['state']== '1'){
				$order[$k]['state_type']='未使用';
				$tab_order_1[]=$order[$k];
			}else if($v['state']== 'p0'){
				$order[$k]['state_type']='未付款';
				$order[$k]['key']='';
				$tab_order_p0[]=$order[$k];
			}
			

		}
		
		

		$this->view->assign('tab_order_0', $tab_order_0);
		$this->view->assign('tab_order_1', $tab_order_1);
		$this->view->assign('tab_order_p0', $tab_order_p0);
		$this->view->assign('order', $order);
		return $this->view->fetch();
	}
	public function set()
	{
		return $this->view->fetch();
	}
	public function setsave()
	{
		$data=input('data');
		$data=json_decode($data,true);
		$where['id']=$data['id'];
		unset($data['id']);
		$data['username']=base64_encode($data['username']);
		
		$info=Db::name('user')->where($where)->update($data);
		if($info){
			return '1';
		}else{
			return '99';
		}
	}
}